package cn.com.nes.site.rocketmq.handler;


import cn.com.nes.mybatis.agent.system.entity.CompanyMerchant;
import cn.com.nes.site.entity.MallUserInfo;
import cn.com.nes.site.module.service.UserService;
import cn.com.nes.site.rocketmq.strategy.MessageHandler;
import cn.com.nes.site.rocketmq.strategy.Topic;
import cn.com.nes.site.service.system.company.CompanyMerchantService;
import cn.hutool.core.util.ObjectUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.rocketmq.common.message.MessageExt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Topic("mall2agent_user_topic")
@Component
public class Mall2AgentUserHandler implements MessageHandler {

    @Autowired
    private CompanyMerchantService companyMerchantService;

    @Autowired
    UserService userService;

    @Override
    public void handleMessage(MessageExt message) {
        System.out.println("处理 mall2agent_user_topic 消息: " + new String(message.getBody()));

        String messageBody = new String(message.getBody());
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            MallUserInfo mallUserInfo = objectMapper.readValue(messageBody, MallUserInfo.class);
            //先根据商铺id查询公司id
            CompanyMerchant companyMerchant = companyMerchantService.selectByCompanyMerchantId(null, mallUserInfo.getMerId().toString());

            if(ObjectUtil.isNotEmpty(companyMerchant.getCompanyId())){
                //根据公司id查询是否已存在超级管理账号 没有则添加超级管理用户,有则添加普通用户
                userService.processSuperAdminUser(companyMerchant.getCompanyId(),null,mallUserInfo);
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
